import { CrossingTrussAlgorithms } from '../../algorithms/crossing/truss/crossingtruss';
import { InvertedTriangularTrussLoadAlgorithms } from '../../algorithms/crossing/truss/crossingtrussload';
import { CrossingTrussExternalLoadAlgorithms } from '../../algorithms/crossing/truss/crossingtrussexternalload';
import { CrossingTrussWeldAlgorithms } from '../../algorithms/crossing/truss/trussweld';
import { ColumnReinforcementAlgorithms } from '../../algorithms/crossing/truss/columnreinforcement';

/**
 * 穿跨越计算服务类，封装穿跨越相关计算功能
 */
export class CrossingService {
  /**
   * 计算地基基础
   * @param params 基础计算参数
   * @returns 基础计算结果
   */
  static async calculateFoundation(params: any) {
    try {
      const {
        foundationLength,
        foundationWidth,
        foundationHeight,
        concreteDensity,
        safetyFactor,
        columnHeight,
        foundationBuriedDepth,
        soilUnitWeight,
        foundationInterfaceCoefficient,
        trussLateralLoad,
        columnWidth,
        columnDepth,
        columnElasticModulus,
        soilCohesion,
        soilFrictionAngle
      } = params;


      const result = CrossingTrussAlgorithms.foundationCalculation(
        foundationLength,
        foundationWidth,
        foundationHeight,
        concreteDensity,
        safetyFactor,
        columnHeight,
        foundationBuriedDepth,
        soilUnitWeight,
        foundationInterfaceCoefficient,
        trussLateralLoad,
        columnWidth,
        columnDepth,
        columnElasticModulus,
        soilCohesion,
        soilFrictionAngle
      );

      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 正三角桁架内力计算
   * @param params 正三角桁架内力计算参数
   * @returns 正三角桁架内力计算结果
   */
  static async triangularTrussCalculation(params: any) {
    try {
      const { length, height, load } = params;



      // 计算正三角桁架内力
      const result = CrossingTrussAlgorithms.triangularTrussCalculation(
         length, height, load 
      );

      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 倒三角桁架内力计算
   * @param params 倒三角桁架内力计算参数
   * @returns 倒三角桁架内力计算结果
   */
  static async invertedTriangularTrussCalculation(params: any) {
    try {
      const { length, height, width, load } = params;

      // 计算倒三角桁架内力
      const result = CrossingTrussAlgorithms.invertedTriangularTrussCalculation(
         length, height, width, load 
      );

      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 矩形桁架内力计算
   * @param params 矩形桁架内力计算参数
   * @returns 矩形桁架内力计算结果
   */
  static async calculateRectangularTrussCalculation(params: any) {
    try {
      const { length, height, width, load } = params;
       
      // 计算矩形桁架内力
      const result = CrossingTrussAlgorithms.rectangularTrussCalculation(
         length, height, width, load 
      );

      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 倒三角桁架自重荷载计算
   * @param params 倒三角桁架荷载计算参数
   * @returns 倒三角桁架荷载计算结果
   */
  static async calculateInvertedTriangularTrussLoad(params: any) {
    try {
      const { 
        length, 
        height, 
        width,
        topChordDiameter,
        bottomChordDiameter,
        webDiameter,
        topChordThickness,
        bottomChordThickness,
        webThickness,
        steelDensity,
        verticalWebSpacing,
        topChordHorizontalDiameter,
        topChordDiagonalDiameter,
        topChordHorizontalThickness,
        topChordDiagonalThickness,
        safetyFactor,
      } = params;
      
      // 计算桁架自重荷载
      const selfWeightResult = InvertedTriangularTrussLoadAlgorithms.calculateSelfWeightLoad(
        length, 
        height, 
        width,
        topChordDiameter,
        bottomChordDiameter,
        webDiameter,
        topChordThickness,
        bottomChordThickness,
        webThickness,
        steelDensity,
        verticalWebSpacing,
        topChordHorizontalDiameter,
        topChordDiagonalDiameter,
        topChordHorizontalThickness,
        topChordDiagonalThickness,
      );
      
      const result = {
        ...selfWeightResult,
      };
      
      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 倒三角桁架外部荷载计算
   * @param params 倒三角桁架外部荷载计算参数
   * @returns 倒三角桁架外部荷载计算结果
   */
  /**
   * 计算管道荷载
   * @param params 管道参数
   * @returns 管道荷载计算结果
   */
  static async calculatePipeLoad(params: any) {
    try {
      const {
        // 管道参数
        pipeOuterDiameter,
        pipeThickness,
        pipeDensity,
        mediumDensity,
        insulationOuterDiameter,
        insulationInnerDiameter,
        insulationDensity,
        equipmentWeight,
        spanLength
      } = params;

      // 管道荷载计算
      const pipeWeightLoad = CrossingTrussExternalLoadAlgorithms.pipeWeightLoad(
        pipeOuterDiameter,
        pipeThickness,
        pipeDensity
      );
      
      const mediumWeightLoad = CrossingTrussExternalLoadAlgorithms.mediumWeightLoad(
        pipeOuterDiameter - 2 * pipeThickness,
        mediumDensity
      );
      
      const insulationWeightLoad = CrossingTrussExternalLoadAlgorithms.insulationWeightLoad(
        insulationOuterDiameter,
        insulationInnerDiameter,
        insulationDensity
      );
      
      const equipmentWeightLoad = CrossingTrussExternalLoadAlgorithms.equipmentWeightLoad(
        equipmentWeight,
        spanLength
      );
      
      const result = {
        pipeWeightLoad,
        mediumWeightLoad,
        insulationWeightLoad,
        equipmentWeightLoad
      };
      
      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 计算外部荷载
   * @param params 外部荷载参数
   * @returns 外部荷载计算结果
   */
  static async calculateExternalLoad(params: any) {
    try {
      const {
        // 环境参数
        windPressure,
        windShapeFactor,
        windHeightFactor,
        windVibrationFactor,
        snowDistributionCoefficient,
        snowPressure,
        openingReductionCoefficient,
        projectionAreaOfSectionsPipesWalkways,
        totalContourAreaOfCalculationDeck,
        maintenanceLoad,
        
        // 温度参数
        pipeOuterDiameter,
        pipeThickness,
        elasticModulus,
        thermalExpansion,
        temperatureDifference
      } = params;
      

      // 环境荷载计算
      const windLoad = CrossingTrussExternalLoadAlgorithms.windLoad(
        windPressure,
        windShapeFactor,
        windHeightFactor,
        windVibrationFactor
      );
      
      const snowLoad = CrossingTrussExternalLoadAlgorithms.snowLoad(
        snowDistributionCoefficient,
        snowPressure,
        openingReductionCoefficient,
        projectionAreaOfSectionsPipesWalkways,
        totalContourAreaOfCalculationDeck
      );
      
      const maintenanceLoadValue = CrossingTrussExternalLoadAlgorithms.maintenanceLoad(
        maintenanceLoad
      );
      
      const temperatureLoad = CrossingTrussExternalLoadAlgorithms.temperatureLoad(
        pipeOuterDiameter,
        pipeThickness,
        elasticModulus,
        thermalExpansion,
        temperatureDifference
      );
      
      const result = {
        windLoad,
        snowLoad,
        maintenanceLoad: maintenanceLoadValue,
        temperatureLoad
      };
      
      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 倒三角桁架外部荷载计算（整合管道和外部荷载）
   * @param params 计算参数
   * @returns 综合计算结果
   */
  static async calculateInvertedTriangularTrussExternalLoad(params: any) {
    try {
      const {
        // 荷载分项系数
        deadLoadFactor,
        liveLoadFactor,
      } = params;
      
      // 计算管道荷载
      const pipeLoadResult = await this.calculatePipeLoad(params);

      
      // 计算外部荷载
      const externalLoadResult = await this.calculateExternalLoad(params);

      
      // 荷载组合
      const combinedLoadResult = CrossingTrussExternalLoadAlgorithms.combinedLoad(
        pipeLoadResult.data?.pipeWeightLoad || 0,
        pipeLoadResult.data?.mediumWeightLoad || 0,
        pipeLoadResult.data?.insulationWeightLoad || 0,
        pipeLoadResult.data?.equipmentWeightLoad || 0,
        externalLoadResult.data?.windLoad || 0,
        externalLoadResult.data?.snowLoad || 0,
        externalLoadResult.data?.maintenanceLoad || 0,
        externalLoadResult.data?.temperatureLoad || 0,
        { dead: deadLoadFactor, live: liveLoadFactor }
      );
      
      const result = {
        ...pipeLoadResult.data,
        ...externalLoadResult.data,
        deadLoad: combinedLoadResult.deadLoad,
        liveLoad: combinedLoadResult.liveLoad,
        combinedLoad: combinedLoadResult.totalLoad
      };
      
      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }

  /**
   * 桁架焊缝计算
   * @param params 焊缝计算参数
   * @returns 焊缝计算结果
   */
  static async trussWeldCalculate(params: any) {
    try {
      const {
        strengthFactor,
        weldSize,
        weldLength,
        weldStrengthValue,
        axialForce
      } = params;

      // 调用焊缝计算算法
      const result = CrossingTrussWeldAlgorithms.calculateWeld(
        strengthFactor,
        weldSize,
        weldLength,
        weldStrengthValue,
        axialForce
      );

      return { success: true, data: result };
    } catch (error: any) {
      return { success: false, error: error.message };
    }
  }
  /**
   * 柱子配筋计算
   * @param params 输入参数
   * @returns 计算结果
   */
  static async columnReinforcement(params: any) {
      try {
        const {
          columnLength,
          columnWidth,
          columnHeight,
          axialForce,
          bottomMoment,
          protectiveLayerThickness,
          shearForce
        } = params;
        
        // 调用实际的柱子配筋计算算法
        const result = ColumnReinforcementAlgorithms.columnReinforcement(
          columnLength,
          columnWidth,
          columnHeight,
          axialForce,
          bottomMoment,
          protectiveLayerThickness,
          shearForce
        );
        
        return { success: true, data: result };
      } catch (error: any) {
        return { success: false, error: error.message };
      }
    }

    
}